﻿using _1._2._2015FullCrud.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _1._2._2015FullCrud.Controllers
{
    public class TodoController : Controller
    {
        //strats our service to store the one service we will use the whole time so we can fake our db
        private TodoService _service;
        public TodoController()
        {
            //sets the private service to a single instance so our info stays till we restart
            _service =  TodoService.Instance;
            
        }
        // GET: Todo
       // [HttpGet] added by default needs changed for post or update
        public ActionResult Index()
        {

            //This is for the index page
            //calls and set a new version of TodoIndexViewModel from the models page
            TodoIndexViewModel model = new TodoIndexViewModel();
          
            //sets model.Todos after getting the todos list from GetTodos in the service page
            model.Todos = _service.GetTodos();
            
            //returns the model with the todos so it can be used by other things like display
            return View(model);
        }

        public ActionResult Create()
        {
            //this is the Get call
            //calls CreateTodoViewodel and starts a new version of it to pass to the next part
            CreateTodoViewModel model = new CreateTodoViewModel();
            //returns it to be used by the next part
            return View(model);
        }
        // Post: create a new todo item
        [HttpPost]
        public ActionResult Create(CreateTodoViewModel model)
        {
            //this is the Post call
            // sends in the model from the last part and calls the function on the instance that we are keeping alive and builds the todo and sets it in the var id
            int id = _service.CreateTodo(model.Text);
            //returns to the index page to display the todo in the list with the rest of them
            return RedirectToAction("Index");
        }

        public ActionResult Update(int id)
        {
            //this is the update page get
            // starts a new version of the UpdateTodoViewModel
            UpdateTodoViewModel model = new UpdateTodoViewModel();

            //calls the TodoViewModel Todo and sets it to the one todo with the id we passed in
            TodoViewModel todo = _service.GetTodo(id);
            //sets the info to be displayed back out to the page and lets us edit it
            model.IsCompleted = todo.IsCompleted;
            model.Id = todo.Id;
            model.Text = todo.Text;

            //returns the new info so it can be sa ved in the next part
            return View(model);
        }
        [HttpPost]
        public ActionResult Update(UpdateTodoViewModel model)
        {
            //this is the Post for the update so we can save the new info

            // calls the instance we have saved and updates the info that we made changes to and set it to done if we checked the box
            _service.UpdateTodo(model.Id, model.Text, model.IsCompleted);
            //returns to the home page so we and not looking at the same old data.
            return RedirectToAction("Index");
        }
    }
}




